package com.epfresh.utils;

import android.util.Log;

import com.epfresh.bean.CitySubTime;
import com.epfresh.bean.ListGoods;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by zhaozhenqiang on 2016/8/5.
 */
public class TimeStringUtils {
    public static void updaTime(List<ListGoods> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        SimpleDateFormat sdf, sdfNew;
        sdf = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss");
        sdfNew = new SimpleDateFormat("MM月dd日");
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getType().equals("1")) {
                try {
                    String pluckingTime = list.get(i).getPluckingTime();
                    if (pluckingTime != null && !"".equals(pluckingTime)) {
                        Date date = sdf.parse(pluckingTime);
                        list.get(i).setPluckingTime(sdfNew.format(date).toString() + "进场");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (list.get(i).getType().equals("2")) {
                try {
                    Date date = sdf.parse(list.get(i).getDeliveryTime());
                    list.get(i).setDeliveryTime("预计" + sdfNew.format(date).toString() + "送达");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return;
      /*  for(int i=0;i<list.size();i++){
            Calendar c = Calendar.getInstance();
            //2016-08-04 14:03:33
            try {
                c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(list.get(i).getModifiedTime()));
            }catch (Exception e){
                e.printStackTrace();
            }
//            System.out.println("时间转化后的毫秒数为：" + c.getTimeInMillis());
            int time = (int)(System.currentTimeMillis()-c.getTimeInMillis())/1000;
            String timeString =getTimeString(time);
            list.get(i).setModifiedTimeString(timeString);
        }*/
    }

    public static String getTimeString(int time) {
        String dateStr;
        if (time <= 60) {  //1分钟以内的
            dateStr = time + "秒前更新";
        } else if (time <= 60 * 60) {  //一个小时以内的
            int mins = time / 60;
            dateStr = mins + "分钟前更新";
        } else if (time <= 60 * 60 * 24) {  //在1天内的
            int hours = time / (60 * 60);
            dateStr = hours + "小时前更新";
        } else if (time <= 60 * 60 * 24 * 30) {
            int days = time / (60 * 60 * 24);
            dateStr = days + "天前更新";
        } else if (time <= 60 * 60 * 24 * 30 * 12) {
            int months = time / (60 * 60 * 24 * 30);
            dateStr = months + "个月前更新";
        } else {
            int years = time / (60 * 60 * 24 * 30 * 12);
            dateStr = years + "年前更新";
        }
        return dateStr;
    }


    public static String getTimeString(String timeString) {
        Calendar c = Calendar.getInstance();
        //2016-08-04 14:03:33
        try {
            c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeString));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("时间转化后的毫秒数为：" + c.getTimeInMillis());
        long time = Math.abs(System.currentTimeMillis() - c.getTimeInMillis()) / 1000;
        if (time <= 60) {  //1分钟以内的
            timeString = time + "秒前更新";
        } else if (time <= 60 * 60) {  //一个小时以内的
            time /= 60;
            timeString = time + "分钟前更新";
        } else if (time <= 60 * 60 * 24) {  //在1天内的
            time /= (60 * 60);
            timeString = time + "小时前更新";
        } else if (time <= 60 * 60 * 24 * 30) {
            time /= (60 * 60 * 24);
            timeString = time + "天前更新";
        } else if (time <= 60 * 60 * 24 * 30 * 12) {
            time /= (60 * 60 * 24 * 30);
            timeString = time + "个月前更新";
        } else {
            time /= (60 * 60 * 24 * 30 * 12);
            timeString = time + "年前更新";
        }
        return timeString;
    }


    public static long formatTimeLong(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
//    public static long getCountDownTime(String time) {
//        try {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date date = sdf.parse(time);
//            return date.getTime();
//        } catch (ParseException e) {
//            e.printStackTrace();
//            return 0;
//        }
//    }

    public static String getTimeLongToString(long time){

        long h = time / (1000 * 3600);
        long m = (time-h*3600*1000) / (1000 * 60);
        long s = (time-h*3600*1000 - m*1000*60) / 1000;

        return String.format("%02d:%02d:%02d",(int)h,(int)m,(int)s);

    }


    public static String getTimeTips(String time){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(time);

            int hours = date.getHours();
            int minutes = date.getMinutes();
            int seconds = date.getSeconds();

            if (seconds == 0 && minutes == 0){
                return  String.format("%02d点截止下单，还剩",hours);
            }else if (seconds == 0){
                return  String.format("%02d点%02d分截止下单，还剩",hours,minutes);
            }else {
                return  String.format("%02d点%02d分截止下单，还剩",hours,minutes);
            }


        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }


    public static String initTime(String timeString, String now){
        Calendar c = Calendar.getInstance();
        //2016-08-04 14:03:33
        try {
            c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeString));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("时间转化后的毫秒数为：" + c.getTimeInMillis());
        long time = Math.abs(System.currentTimeMillis() - c.getTimeInMillis()) / 1000;
        if (time <= 60) {  //1分钟以内的
            timeString = time + "秒前";
            //timeString = "0分钟前";
        } else if (time <= 60 * 60) {  //一个小时以内的
            time /= 60;
            timeString = time + "分钟前";
        } else if (time <= 60 * 60 * 24) {  //在1天内的
            time /= (60 * 60);
            timeString = time + "小时前";
        } else if (time <= 60 * 60 * 24 * 30) {
            time /= (60 * 60 * 24);
            if(time>5)
                time = 5;
            timeString = time + "天前";
        } /*else if (time <= 60 * 60 * 24 * 30 * 12) {
            time /= (60 * 60 * 24 * 30);
            timeString = time + "个月前";
        } */else {
            //time /= (60 * 60 * 24 * 30 * 12);
            //timeString = time + "年前";
            timeString = "5天前";
        }
        return timeString;
    }


}
